package com.changzhi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changzhi.common.annotation.Verify;
import com.changzhi.common.util.Result;
import com.changzhi.ldyl.model.AppVersion;
import com.changzhi.ldyl.model.dto.BaseDto;
import com.changzhi.ldyl.model.dto.LoveDto;
import com.changzhi.ldyl.model.vo.LoveVo;
import com.changzhi.mapper.AppVersionMapper;
import com.changzhi.service.LoveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:光棍节双11活动
 * @author: 小落
 * @date: 2020年11月02日
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("love")
public class LoveController {

	@Autowired
	private LoveService loveService;
	@Resource
	private AppVersionMapper appVersionMapper;


	/**
	 * 获取告白墙
	 * @param loveDto
	 * @return
	 */
	@Verify(login = false)
	@PostMapping("getConfessionWallList")
	public Result getConfessionWallList(@RequestBody @Validated LoveDto loveDto){
		long start = System.currentTimeMillis();
		List<LoveVo> confessionWallList = loveService.getConfessionWallList(loveDto);
		log.info("methodName:getConfessionWallList,耗时={}", System.currentTimeMillis() - start);
		return Result.success(confessionWallList);
	}

	/**
	 * 获取待告白用户
	 * @param loveDto
	 * @return
	 */
	@Verify
	@PostMapping("getConfessionUserList")
	public Result getConfessionUserList(@RequestBody LoveDto loveDto){
		long start = System.currentTimeMillis();
		List<LoveVo> confessionUserList = loveService.getConfessionUserList(loveDto);
		log.info("methodName:getConfessionUserList,耗时={}", System.currentTimeMillis() - start);
		return Result.success(confessionUserList);
	}


	/**
	 * 告白
	 * @param loveDto
	 * @return
	 */
	@Verify
	@PostMapping("confession")
	public Result confession(@RequestBody LoveDto loveDto){
		long start = System.currentTimeMillis();
		loveService.confession(loveDto);
		log.info("methodName:confession,耗时={}", System.currentTimeMillis() - start);
		return Result.success();
	}

	/**
	 * 获取排行榜
	 * @param loveDto
	 * @return
	 */
	@Verify(login = false)
	@PostMapping("getRankingList")
	public Result getRankingList(@RequestBody LoveDto loveDto){
		long start = System.currentTimeMillis();
		List<LoveVo> rankingList = loveService.getRankingList(loveDto);
		log.info("methodName:getRankingList,耗时={}", System.currentTimeMillis() - start);
		return Result.success(rankingList);
	}

	/**
	 * 获取奖励
	 * @param loveDto
	 * @return
	 */
	@Verify
	@PostMapping("getRewardInfo")
	public Result getRewardInfo(@RequestBody LoveDto loveDto){
		long start = System.currentTimeMillis();
		LoveVo rewardInfo = loveService.getRewardInfo(loveDto);
		log.info("methodName:getRewardInfo,耗时={}", System.currentTimeMillis() - start);
		return Result.success(rewardInfo);
	}

	/**
	 * 领取礼物
	 * @param loveDto
	 * @return
	 */
	@Verify
	@PostMapping("receive")
	public Result receive(@RequestBody LoveDto loveDto){
		long start = System.currentTimeMillis();
		Map<String, Object> map = loveService.receiveGift(loveDto);
		log.info("methodName:receive,耗时={}", System.currentTimeMillis() - start);
		return Result.success(map);
	}


	@Verify(login = false)
	@PostMapping("/getDownloadUrl")
	public Result getDownloadUrl(@RequestBody BaseDto baseDto) {
		long start = System.currentTimeMillis();
		QueryWrapper<AppVersion> wrapper =  new QueryWrapper<>();
		wrapper.lambda().eq(AppVersion::getOpenId,"official");
		AppVersion appVersion = appVersionMapper.selectOne(wrapper);
		Map<String,Object> map = new HashMap<>();
		map.put("url", appVersion == null ? "" :appVersion.getDownloadUrl());
		log.info("methodName:getDownloadUrl,耗时={}", System.currentTimeMillis() - start);
		return Result.success(map);
	}



}
